package controller.wetch;


import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import controller.wetch.menu.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import util.WeixinMenuUtil;

public class MenuManager {
    private static Logger log = LoggerFactory.getLogger(MenuManager.class);

    public static void main(String[] args) {
        // 第三方用户唯一凭证

        String appId = "wx10e7fe5836ab4a58";
        // 第三方用户唯一凭证密钥

        String appSecret = "72289bf0b97f9fca74356d88f7c43b48";

        // 调用接口获取access_token

        AccessToken at = WeixinMenuUtil.getAccessToken(appId, appSecret);

        if (null != at) {
            // 调用接口创建菜单

            int result = WeixinMenuUtil.createMenu(JSONObject.toJSONString(getMenu()), at.getAccessToken());

            // 判断菜单创建结果

            if (0 == result)
                log.info("菜单创建成功！");
            else
                log.info("菜单创建失败，错误码：" + result);
        }
    }

    /**

     * 组装菜单数据

     *

     * @return

     */
    private static Menu_ getMenu() {
/*
        String GET_CODE = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=CORPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_base&state=a123#wechat_redirect";
        String get_code_url = GET_CODE.replace("CORPID", "wx82215c1b2ff61764").replace("REDIRECT_URI", URLEncoder.encode("http://fhs.vhuomai.cn/"));
        System.out.println(get_code_url);*/
        //https://open.weixin.qq.com/connect/oauth2/authorize?appid=ww0e7be2a6a0200924&redirect_uri=114.242.158.130%3A10086%2FGetCode&response_type=code&scope=snsapi_base&state=a123#wechat_redirect

        CommonButton btn11 = new CommonButton();
        btn11.setName("管理平台");
        btn11.setType("view");
        String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx82215c1b2ff61764&redirect_uri=";
        try {
            url += URLEncoder.encode("http://fhs.vhuomai.cn/initPage", "UTF-8")+"&response_type=code&scope=snsapi_base&state=1&#wechat_redirect";
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println(url);
        btn11.setUrl(url);
        /**
         * 微信：  mainBtn1,mainBtn2,mainBtn3底部的一级菜单。
         */

        ComplexButton mainBtn1 = new ComplexButton();
        mainBtn1.setName("生活助手");
        mainBtn1.setSub_button(new CommonButton[] { btn11 });
        mainBtn1.setSub_button(new CommonButton[] { btn11 });

        Menu_ menu_ = new Menu_();
        menu_.setButton(new Button[] {mainBtn1});
        Object json = JSONObject.toJSON(menu_);
        return menu_;
    }
}